package com.qf.web.servlet;

import com.qf.pojo.Cart;
import com.qf.pojo.Goods;
import com.qf.pojo.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {

    CartService cartService = new CartServiceImpl();

    public void clearCartAjax(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        cartService.clearCart(user.getId());

        response.getWriter().print("success");
    }

    public String getCart(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        Object user = request.getSession().getAttribute("user");
        if(user == null) {
            return "redirect: /login.jsp";
        }
        User u = (User) user;
        //获取当前用户的购物车  ： 根据用户id 查询购物车
        List<Cart> carts = cartService.findCartByUId(u.getId());
        //存储数据到请求域
        request.setAttribute("carts", carts);

        return "forward:/cart.jsp";

    }
    //购物车商品： 数量加一/减一
    public void addCartAjax(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");

        //根据商品id获取商品对象
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));
        //根据用户id和商品获取获取购物车信息
        Cart cart = cartService.findByUidAndGid(user.getId(), Integer.parseInt(goodsId));
        if(cart == null) {
            cart = new Cart();
            //用户id
            cart.setId(user.getId());
            cart.setPid(Integer.parseInt(goodsId));
            cart.setMoney(Integer.parseInt(number) * goods.getPrice());
            cart.setNum(Integer.parseInt(number));

            cartService.insert(cart);
        }else {
            //如果number 则删除购物车记录
            if(Integer.parseInt(number) == 0) {
                cartService.deleteCart(user.getId(), Integer.parseInt(goodsId));
            } else {
                cart.setNum(cart.getNum() + Integer.parseInt(number));
                cart.setMoney(goods.getPrice() * cart.getNum());
                cartService.updateCart(cart);
            }

        }

        response.getWriter().print("success");
    }

    public String addCart(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        Object userObj = request.getSession().getAttribute("user");
        if(userObj == null) {
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if(StrUtils.isEmpty(goodsId) || StrUtils.isEmpty(number)){
            request.setAttribute("msg", "添加到购物车失败！！");
            return "forward:/message.jsp";
        }
        User user = (User) userObj;

        //根据商品id 查询商品对象
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));

        //查询该用户有没有添加此商品到购物车， 如果此商品已经在购物车，更新，如果不在，则新增
        Cart cart = cartService.findByUidAndGid(user.getId(), Integer.parseInt(goodsId));

        //购物车不存在，则添加
        if(cart == null) {
            cart = new Cart();
            //用户id
            cart.setId(user.getId());
            cart.setPid(Integer.parseInt(goodsId));
            cart.setMoney(Integer.parseInt(number) * goods.getPrice());
            cart.setNum(Integer.parseInt(number));

            cartService.insert(cart);

        }
        //此商品在购物车存在，则修改
        else {
            cart.setNum(cart.getNum() + Integer.parseInt(number));
//            cart.setMoney(new BigDecimal(goods.getPrice()).multiply(new BigDecimal(cart.getNum())).intValue());
            cart.setMoney(goods.getPrice() * cart.getNum());

            cartService.updateCart(cart);
        }



        return "redirect:/cartSuccess.jsp";
    }




}